package com.tidemark.ui.console;

import java.util.Properties;

import com.tidemark.StockDataModel;

public class MainConsole {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		parseArgs(args);
		
		new Thread(){
			 public void run(){
				 StockDataModel model = new com.tidemark.ui.console.ConsoleDataModel();
				 //com.tidemark.Reader r1 = new com.ifeng.stock.AbsoluteReader(model);  //AbsoluteReader is old algorithm
				 //com.tidemark.Reader r2 = new com.tidemark.dingniu8.Reader(model); //dingniu8 website has changed 
				 com.tidemark.Reader r3 = new com.ifeng.stock.RelativeReader(model);

				 while(true){ //in console mode, auto refresh every 20 seconds
					//r1.propose();  
					//r2.propose();
					r3.propose();
					
					try {
						Thread.sleep(1000*20);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				 }
			 }
		}.start();
	}

	private static void parseArgs(String[] args) {
		
		if(args == null || args.length == 0) {
			return;
		}
		
		for(int i = 0; i < args.length; i++) {
			if(args[i].equalsIgnoreCase("-proxy")) {
				i++;
				if(i < args.length) {
					setProxy(args[i]);
				}
			} else if(args[i].equalsIgnoreCase("-h")) {
				showHelp();
			}
		}
	}
	
	private static void showHelp() {
		System.out.println("Usage:\r\n -c : print code only \r\n -n : print name only\r\n");
	}
	
	//setup proxy arguments, format is: host:port, such as: proxy.stsk:8080
	private static void setProxy(String proxyString) {
		String proxy;
		String port;
		int index = proxyString.indexOf(":");
		if(index < 0) {
			proxy = proxyString;
			port = "8080";
		} else {
			proxy = proxyString.substring(0, index);
			port = proxyString.substring(index+1);
		}
		Properties sys = System.getProperties();
		sys.put("proxySet","true");
		sys.put("proxyHost",proxy);
		sys.put("proxyPort",port);
		System.setProperties(sys);
	} 
}
